package com.example.sunran.service.impl;

import com.example.sunran.service.FTPService;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPListParseEngine;
import org.apache.commons.net.ftp.FTPReply;

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class FTPServiceImpl implements FTPService {

    private FTPClient ftpClient = null;

    public boolean initFtp(String hostname, String user, String pwd, int port)  {

        try {
            ftpClient = new FTPClient();
            ftpClient.setControlEncoding("utf-8");
            ftpClient.connect(hostname, port);
            ftpClient.login(user, pwd);
            int replyCode = ftpClient.getReplyCode();
            System.out.println(replyCode);
            if (!FTPReply.isPositiveCompletion(replyCode)) {
                return false;
            }else{
                return true;

            }

        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;

    }

    public int uploadFile(String pathname, String fileName,File originFile) {

        InputStream inputStream = null;
        try{
            System.out.println("开始上传文件");
            inputStream = new FileInputStream(originFile);
            ftpClient.setFileType(ftpClient.BINARY_FILE_TYPE);
            ftpClient.makeDirectory(pathname);
            ftpClient.changeWorkingDirectory(pathname);
            ftpClient.storeFile(fileName, inputStream);
            inputStream.close();
            System.out.println("上传文件成功");
        }catch (Exception e) {
            System.out.println("上传文件失败");
            e.printStackTrace();
        }
        return 1;
    }

    public boolean createFolder(String parentPathh, String folderName) {
        return false;
    }

    public void downloadFile(String pathname, String filename, String localpath) {
        OutputStream os=null;
        try {
            System.out.println("开始下载文件");
            //切换FTP目录
            ftpClient.changeWorkingDirectory(pathname);
            FTPFile[] ftpFiles = ftpClient.listFiles();
            for(FTPFile file : ftpFiles){
                if(filename.equalsIgnoreCase(file.getName())){
                    File localFile = new File(localpath + "/" + file.getName());
                    os = new FileOutputStream(localFile);
                    ftpClient.retrieveFile(file.getName(), os);
                    os.close();
                }
            }
            System.out.println("下载文件成功");
        } catch (Exception e) {
            System.out.println("下载文件失败");
            e.printStackTrace();
        }


    }

    @Override
    public List<String> getFtpFile() {

        try {
            return Arrays.asList(ftpClient.listNames());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new ArrayList<>();
    }

    public static void main(String[] args) {
        FTPService ftpService = new FTPServiceImpl();

        boolean state = ftpService.initFtp("127.0.0.1", "ftp", "ftp", 21);
    }

    @Override
    public List<File> getLocalFile(String filePath) {

        File file = new File(filePath);
        if (!file.exists()) {
            return new ArrayList<>();
        }
        return Arrays.asList(file.listFiles());
    }
}
